<?php
/**
 * @Author: chizh
 * @Date:   2017-02-20 21:24:13
 * @Last Modified by:   qi
 * @Last Modified time: 2017-02-23 11:25:34
 */
namespace frontend\modules\controllers;
use Yii;
use yii\web\Controller;
use frontend\modules\models\Admin;
use yii\data\Pagination;
/**
 * Default controller for the `admin` module
 */
class ManageController extends Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionMailchangepass()
    {	
    	$this->layout = false;
     	$time =Yii::$app->request->get('timestamp');
     	$adminuser = Yii::$app->request->get('adminuser');
     	$token = Yii::$app->request->get('token');
     	$model = new Admin;
     	$myToken = $model->createToken($adminuser, $time);
     	if($myToken != $token){
     		$this->redirect(['public/login']);
     		Yii::$app->end();
     	}
     	if(time()-$time>300){
     		$this->redirect(['public/login']);
     		Yii::$app->end();
     	}
     	if(Yii::$app->request->isPost){
     		$post = Yii::$app->request->post();
     		$res = $model->changePass($post);
     		if($res){
     			Yii::$app->session->setFlash('info','密码修改成功');
     			$this->redirect(['public/login']);
     		}
     	}
     	$model->adminuser = $adminuser;
     	return $this->render("mailchangepass",['model'=>$model]);
    }

    public function actionManagers(){
        $this->layout = 'layout';
        $model = Admin::find();
        $count = $model->count();
        $pageSize = Yii::$app->params['pageSize']['manage'];
        $pager = new Pagination(['totalCount'=>$count,'pageSize'=>$pageSize]);
        $managers = $model->offset($pager->offset)->limit($pager->limit)->all();
        return $this->render("managers",['managers'=>$managers,'pager'=>$pager]);
    }

    public function actionReg(){
        $this->layout = 'layout';
        $model = new Admin;
        if(Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            if($model->reg($post)){
                Yii::$app->session->setFlash('info','添加成功');
            }else{
                Yii::$app->session->setFlash('info','添加失败');
            }
        }
        $model->adminpass='';
        $model->repass="";
        return $this->render('reg',['model'=>$model]);
    }

    public function actionDel(){

        $adminid = Yii::$app->request->get('adminid');
        if(empty($adminid)){
            $this->redirect(['manage/managers']);
        }
        $model = new Admin;
        if($model->deleteAll('adminid=:id',[':id'=>$adminid])){
            Yii::$app->session->setFlash('info','删除成功');
            $this->redirect(['manage/managers']);
        }else{
            Yii::$app->session->setFlash('info','删除失败');
        }
       
        $model->adminpass='';
        $model->repass="";
        return $this->render('reg',['model'=>$model]);
    }

    public function actionChangeemail(){
        $this->layout = 'layout';
        //$info = Yii::$app->session;
        $cookie = Yii::$app->request->cookies;
        $info = $cookie->getValue('admin');
        $model = Admin::find()->where('adminuser = :user',[':user'=>$info['adminuser']])->one();
        if(Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            if($model->changeemail($post)){
                Yii::$app->session->setFlash('info','修改成功');
            }
        }
        $model->adminpass='';
        return $this->render('changeemail',['model'=>$model]);
    }

    public function actionChangepass(){
        $this->layout = 'layout';
        $cookie = Yii::$app->request->cookies;
        $info = $cookie->getValue('admin');
        $model=Admin::find()->where('adminuser=:user',[':user'=>$info['adminuser']])->one();
        $model->adminpass='';
        $model->repass='';
        if(Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            if($model->changepass($post)){
                Yii::$app->session->setFlash('info','修改成功');
            }
        }
        return $this->render('changePass',['model'=>$model]);
    }



}
